#include "splitter.h"


#include <QBoxLayout>
#include <QSplitter>
#include "imitatorwidget.h"



USplitter::USplitter(Qt::Orientation orient, QWidget *parent):IndexedContainer(parent)
{
    m_splitter = new QSplitter(orient,this);
    m_splitter->setChildrenCollapsible(false);
    QBoxLayout* layout = new QHBoxLayout();
    layout->setMargin(0);
    layout->addWidget(m_splitter);
    this->setLayout(layout);

}

int USplitter::dockingIndex(DockOption dockoption, QWidget *relativeAnchor)
{
    if (!(dockoption & DockOption::CROSS)){
        return -1;
    }
    if (this->orientation() == Qt::Horizontal){
        if (!(dockoption & DockOption::Horizontal))
            return -1;
    } else {
        if (!(dockoption & DockOption::Vertical))
            return -1;
    }
    if (dockoption & (DockOption::Left | DockOption::Top)){
        return relativeAnchor ? indexOf(relativeAnchor) : 0;
    }
    return relativeAnchor ? indexOf(relativeAnchor) + 1 : this->count();
}

//USplitter::~USplitter()
//{

//}

void USplitter::insertWidget(int index, QWidget *widget)
{
    //TODO: create better sizing policy
    m_splitter->insertWidget(index,widget);
}

void USplitter::removeWidget(QWidget *widget)
{
    widget->setParent(0);
    if (count() == 1){//the splitter is now pointless
        QDockItem* parent = parentDockItem();
        QDockItemContainer* container = qobject_cast<QDockItemContainer*>(parent);
        if (container){
            container->replaceWidget(this,this->widgetAt(0));
        }
//        QBaseDock
        this->destroy();
    }
}

int USplitter::indexOf(QWidget *widget)
{
    return m_splitter->indexOf(widget);
}

QWidget *USplitter::widgetAt(int index)
{
    return m_splitter->widget(index);
}

void USplitter::replaceWidget(QWidget *old_widgets, QWidget *new_widget)
{
    auto tmp = m_splitter->saveState();
    IndexedContainer::replaceWidget(old_widgets, new_widget);
    m_splitter->restoreState(tmp);
}

bool USplitter::isHandlerFor(DockOption dockoption)
{
    if (dockoption & DockOption::Vertical)
        return this->orientation() == Qt::Vertical;
    return this->orientation() == Qt::Horizontal;
}

QRect USplitter::simulateDrop(QDockItem *dragged_widget, QWidget *target_widget, DockOption dockoption)
{
    if (this->orientation() == Qt::Vertical){
        if (!(dockoption & DockOption::Vertical)){
//            qDebug() << "not simulated";
            return QRect();
        }
    }
    else if (this->orientation() == Qt::Horizontal){
        if (!(dockoption & DockOption::Horizontal)){
//            qDebug() << "not simulated";
            return QRect();
        }
    }

    QRect rec;
    ImitatorWidget* imitated_dragged = new ImitatorWidget(dragged_widget);
    imitated_dragged->deleteLater();
//    auto state = m_splitter->saveState();
    auto sizes = m_splitter->sizes();
    m_splitter->setSizes(sizes);

    int index = this->indexOf(target_widget);
    if (index == -1)
        index = 0;
    if (dockoption & DockOption::Right || dockoption & DockOption::Bottom){
        index++;
        if (target_widget == this){
            index = this->count();
        }
    }
    this->insertWidget(index,imitated_dragged);

    m_splitter->refresh();
    QPixmap::grabWidget(this);


//    qDebug()<< "in simulation ";

    rec = imitated_dragged->geometry();
    //    QRect thisrect = this->geometry();
    rec.moveTopLeft(this->mapToGlobal(rec.topLeft()));
    if (this->orientation() == Qt::Vertical){
        rec.setWidth(this->width());
    }
    else {//Horizontal
        rec.setHeight(this->height());
    }

    this->removeWidget(imitated_dragged);

//    m_splitter->restoreState(state);
    m_splitter->setSizes(sizes);
    return rec;

}

